<?php


//require_once ('html/htmlHelper.php');
//require_once ('view/portfolioView.php');
//require_once ('view/projectView.php');
//require_once ('display/testChart.php');
require_once 'session/access.php';
include 'model/LoginModel.php';
//require_once 'utility.php';

require 'view/LoginView.php';
//controllers
require_once 'controller/loginController.php';
require_once 'controller/notesControllerClass.php';
require_once 'controller/projectControllerClass.php';
require_once 'controller/portfolioControllerClass.php';
require_once 'view/taskViewClass.php';
require_once 'view/notesViewClass.php';
require_once 'view/subtaskViewClass.php';
require_once 'view/portfolioViewClass.php';
require_once 'model/notesModelClass.php';
if (!isAuthorized("mainPage.php")) {
    header("location: notAuthorized.php?page=mainPage");
}

$requestType = $_GET['request'];
$action = $_GET['action'];
if (isset($_GET['projectId'])) {
    $projectID = $_GET['projectId'];
}

if (strcmp($requestType, "login") === 0) {
//    if (strcmp(getUserRole(), "piapprover")===0) {
//        $model = new portfolioModelClass();
//        $controller = new portfolioControllerClass($model);
//        $view = new LoginView($controller, $model);
//    } else {
        $model = new LoginModel();
        $controller = new loginController($model);
        $view = new LoginView($controller, $model);
    //}
    $controller->{$action}();
    $view->output();
} 

else if (strcmp($requestType, "portfolio") === 0) {
    //instantiate an empty model class
    $model = new portfolioModelClass();
    $controller = new portfolioControllerClass($model);
    $view = new portfolioViewClass($controller, $model);
    $controller->{$action}();
    $view->output();
} 

else if (strcmp($requestType, "project") === 0) {
    //instantiate an empty model class
    $model = new projectModelClass();
    $controller = new projectControllerClass($model);
    $view = new projectViewClass($controller, $model);
    $controller->{$action}();
    $view->output();
} 

else if (strcmp($requestType, "notes") === 0) {
    //instantiate an empty model class
    $model = new notesModelClass();
    $controller = new notesControllerClass($model);
    $view = new notesViewClass($controller, $model);
    $controller->{$action}();
    $view->output();
} 

else if (strcmp($requestType, "task") === 0) {
    $model = new projectModelClass();
    if (isset($_GET['projectId'])) {
        $projectId = $_GET['projectId'];
        $model->init($projectId, 1);
    }
    $taskID = $_GET['taskId'];
    $taskModel = $model->getProjectTask($taskID);
    $controller = new taskControllerClass($taskModel);
    $view = new taskViewClass($controller, $taskModel);
    $controller->{$action}();
    $view->output();
} 

else if (strcmp($requestType, "subtask") === 0) {

    if (isset($_GET['subtaskId'])) {
        $subtaskID = $_GET['subtaskId'];
    } else {
        $subtaskID = $_POST['subtaskId'];
    }
    //echo $subtaskID;
    $taskID = getFieldGivenAnID("subtask", "subtaskId", $subtaskID, "taskID");
    //echo $taskID;
    $projectID = getFieldGivenAnID("task", "taskId", $taskID, "projectID");
    //echo $projectID;
    $model = new projectModelClass();
    $model->init($projectID, 2);
    $taskModel = $model->getProjectTask($taskID);
    $subtaskModel = $taskModel->getTaskSubtask($subtaskID);
    //echo $subtaskModel->ID;
    $controller = new subtaskControllerClass($subtaskModel);
    $view = new subtaskViewClass($controller, $subtaskModel);
    //return;
    $controller->{$action}();
    $view->output();
}
//common model
else{
    $model = new commonModel();
    $controller = new commonController($model);
    $view = new projectViewClass($controller, $model);
    $controller->{$action}();
    $view->output();
}
?>